package com.fanxuankai.boot.monitor.jdbc;

import com.fanxuankai.boot.monitor.BaseMonitorProperties;
import com.fanxuankai.boot.monitor.autoconfigure.MonitorProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * DataBase Checksum
 *
 * @author fanxuankai
 */
@ConfigurationProperties(prefix = ChecksumProperties.PREFIX)
public class ChecksumProperties extends BaseMonitorProperties<ChecksumProperties.MasterSlave> {
    public static final String PREFIX = MonitorProperties.PREFIX + ".jdbc.checksum";
    /**
     * 全局阈值
     */
    private int globalThreshold;

    public ChecksumProperties() {
        setTitle("DataBase Checksum");
        setCron("0 0/5 * * * ?");
    }

    public int getGlobalThreshold() {
        return globalThreshold;
    }

    public void setGlobalThreshold(int globalThreshold) {
        this.globalThreshold = globalThreshold;
    }

    public static class MasterSlave {
        /**
         * schema 列表
         */
        private Set<String> schemas;
        /**
         * 阈值
         * schema: { table: threshold}
         */
        private Map<String, Map<String, Integer>> threshold = Collections.emptyMap();
        /**
         * 包含表，优先级高于 excludeTables 并与其互斥，两者二选一
         * schema: [table]
         */
        private Map<String, Set<String>> includeTables = Collections.emptyMap();
        /**
         * 排除表
         * schema: [table]
         */
        private Map<String, Set<String>> excludeTables = Collections.emptyMap();
        /**
         * 主节点
         */
        private DataBase master;
        /**
         * 从节点列表
         */
        private List<DataBase> slaves;

        public Map<String, Map<String, Integer>> getThreshold() {
            return threshold;
        }

        public void setThreshold(Map<String, Map<String, Integer>> threshold) {
            this.threshold = threshold;
        }

        public Set<String> getSchemas() {
            return schemas;
        }

        public void setSchemas(Set<String> schemas) {
            this.schemas = schemas;
        }

        public Map<String, Set<String>> getIncludeTables() {
            return includeTables;
        }

        public void setIncludeTables(Map<String, Set<String>> includeTables) {
            this.includeTables = includeTables;
        }

        public Map<String, Set<String>> getExcludeTables() {
            return excludeTables;
        }

        public void setExcludeTables(Map<String, Set<String>> excludeTables) {
            this.excludeTables = excludeTables;
        }

        public DataBase getMaster() {
            return master;
        }

        public void setMaster(DataBase master) {
            this.master = master;
        }

        public List<DataBase> getSlaves() {
            return slaves;
        }

        public void setSlaves(List<DataBase> slaves) {
            this.slaves = slaves;
        }
    }

    public static class DataBase {
        private String jdbcUrl;
        private String username;
        private String password;

        public String getJdbcUrl() {
            return jdbcUrl;
        }

        public void setJdbcUrl(String jdbcUrl) {
            this.jdbcUrl = jdbcUrl;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
}
